﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SVAP
{
    class ModifyFre
    {
        private static List<string> listRecentPic = new List<string>();
        private static List<string> listRecentVideo = new List<string>();
        bool addToRecentPic;
        bool addToRecentVideo;
        char[] splitChar = { '*' };

        internal void ModifyFrePic(string fileName)
        {
            addToRecentPic = true;
            string strFromPath = System.AppDomain.CurrentDomain.BaseDirectory;
            strFromPath += @"\RecentPictures.txt";
            StreamReader sr = new StreamReader(strFromPath, Encoding.Default);
            string sLine = "";
            while (sLine != null)
            {
                sLine = sr.ReadLine();
                if (sLine != null)
                    listRecentPic.Add(sLine);
            }
            sr.Close();

            foreach (string str in listRecentPic)
            {
                //if (str == fileName)
                if(str.IndexOf(fileName)>-1)
                {
                    //修改对应行的频率信息
                    string[] strSplit = str.Split(splitChar);
                    //int.Parse(strSplit[1].ToString())++;
                    int i = int.Parse(strSplit[1].ToString());
                    i++;
                    //str = strSplit[0] + "*" + (++int.Parse(strSplit[1].ToString())).ToString();
                    //str = strSplit[0] + "*" + i.ToString();
                    listRecentPic.Remove(str);
                    listRecentPic.Add(strSplit[0] + "*" + i.ToString());

                    addToRecentPic = false;
                    break;
                }
            }

            if(addToRecentPic)
            {
                FileStream fs = new FileStream(strFromPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs,Encoding.Default);
                sw.WriteLine(fileName + "*1");
                sw.Flush();
                sw.Close();
                fs.Close();
            }
            else
            {
                FileStream fss = new FileStream(strFromPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                fss.SetLength(0);
                StreamWriter sww = new StreamWriter(fss,Encoding.Default);
                sww.Write("");

                sww.Flush();
                sww.Close();
                fss.Close();

                ////////////////////////////////////////////////////////////////////
                FileStream fs = new FileStream(strFromPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);

                foreach (string str in listRecentPic)
                {
                    sw.WriteLine(str);
                }

                sw.Flush();
                sw.Close();
                fs.Close();
                
            }
        }


        internal void ModifyFreVideo(string fileName)
        {
            addToRecentVideo = true;
            string strFromPath = System.AppDomain.CurrentDomain.BaseDirectory;
            strFromPath += @"\RecentVideos.txt";
            StreamReader sr = new StreamReader(strFromPath, Encoding.Default);
            string sLine = "";
            while (sLine != null)
            {
                sLine = sr.ReadLine();
                if (sLine != null)
                    listRecentVideo.Add(sLine);
            }
            sr.Close();

            foreach (string str in listRecentVideo)
            {
                //if (str == fileName)
                if (str.IndexOf(fileName) > -1)
                {
                    //修改对应行的频率信息
                    string[] strSplit = str.Split(splitChar);
                    //int.Parse(strSplit[1].ToString())++;
                    int i = int.Parse(strSplit[1].ToString());
                    i++;
                    //str = strSplit[0] + "*" + (++int.Parse(strSplit[1].ToString())).ToString();
                    //str = strSplit[0] + "*" + i.ToString();
                    listRecentVideo.Remove(str);
                    listRecentVideo.Add(strSplit[0] + "*" + i.ToString());

                    addToRecentVideo = false;
                    break;
                }
            }

            if (addToRecentVideo)
            {
                FileStream fs = new FileStream(strFromPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);
                sw.WriteLine(fileName + "*1");
                sw.Flush();
                sw.Close();
                fs.Close();
            }
            else
            {
                FileStream fss = new FileStream(strFromPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read);
                fss.SetLength(0);
                StreamWriter sww = new StreamWriter(fss, Encoding.Default);
                sww.Write("");

                sww.Flush();
                sww.Close();
                fss.Close();

                ////////////////////////////////////////////////////////////////////
                FileStream fs = new FileStream(strFromPath, FileMode.Append);
                StreamWriter sw = new StreamWriter(fs, Encoding.Default);

                foreach (string str in listRecentVideo)
                {
                    sw.WriteLine(str);
                }

                sw.Flush();
                sw.Close();
                fs.Close();

            }
        }
 
    }
}
